var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/BMap/index.tsx
var BMap_exports = {};
__export(BMap_exports, {
  InputBMap: () => InputBMap,
  openBMap: () => openBMap
});
module.exports = __toCommonJS(BMap_exports);
var import_react = __toESM(require("react"));
var import_baseComponent = require("../../baseComponent");
var import_util = require("../../util");
async function openBMap(props) {
  await (0, import_util.initBMapSource)(props.ak);
  return new Promise((resolve) => {
    const apiRef = import_react.default.createRef();
    const handleOk = async (ins) => {
      var _a;
      const value = ((_a = apiRef.current) == null ? void 0 : _a.getValue()) ?? {};
      ins.destroy();
      resolve({
        value,
        closeStatus: "ok"
      });
    };
    const handleCancel = (ins) => {
      var _a;
      const value = ((_a = apiRef.current) == null ? void 0 : _a.getValue()) ?? {};
      ins.destroy();
      resolve({
        value,
        closeStatus: "cancel"
      });
    };
    import_util.core.external.showModal({
      title: "地图",
      width: 1e3,
      height: 700,
      body: { position: "relative" },
      content: /* @__PURE__ */ import_react.default.createElement(import_util.BMap, { ref: apiRef, ...props }),
      async onOk(ins) {
        await handleOk(ins);
      },
      async onCancel(ins) {
        await handleCancel(ins);
      }
    });
  });
}
var InputBMap = (0, import_util.compHoc)((p) => {
  const { value, onChange, outRef, labelInValue, labelField = "address", disabled, ...props } = p;
  const valueToJson = () => labelInValue ? import_util.core.parseJson(value) : value;
  const [state, setState] = (0, import_util.useRefState)({ loading: false, value: valueToJson() });
  (0, import_util.useUpdateEffect)(() => {
    setState({ value: valueToJson() });
  }, [value]);
  const onTrigger = async () => {
    const mapProps = { ...props };
    if (state.value) {
      if (labelInValue) {
        mapProps.point = state.value.point;
        if (!mapProps.point) {
          mapProps.address = state.value.address || state.value;
        }
      } else {
        mapProps.address = state.value;
      }
    }
    setState({ loading: true });
    const { closeStatus, value: v } = await openBMap(mapProps);
    if (closeStatus === "ok") {
      const data = {
        ...v.address_detail,
        ...v.point,
        point: v.point,
        address: v.address,
        business: v.business
      };
      const newValue = labelInValue ? import_util.core.jsonString(data) : data[labelField];
      if (!p.hasOwnProperty("value") || !p.hasOwnProperty("onChange")) {
        setState({ value: newValue });
      }
      onChange == null ? void 0 : onChange(newValue, data);
    }
    setState({ loading: false });
  };
  const onClear = (v) => {
    !v && setState({ value: v });
  };
  (0, import_util.useExtendRef)(outRef, () => {
    return {
      getValue() {
        return state.value;
      },
      setValue(v) {
        if (state.value === v) {
          return;
        }
        setState({ value: v });
        onChange == null ? void 0 : onChange(v);
      }
    };
  });
  const getText = () => {
    var _a;
    return labelInValue ? ((_a = state.value) == null ? void 0 : _a[labelField]) || state.value : state.value;
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_baseComponent.InputTrigger,
    {
      disabled,
      suffix: state.loading ? /* @__PURE__ */ import_react.default.createElement(import_util.LoadingOutlined, null) : /* @__PURE__ */ import_react.default.createElement(import_util.EnvironmentOutlined, null),
      editable: false,
      onChange: onClear,
      value: getText(),
      onTrigger
    }
  );
});
(0, import_util.registerComponent)({ InputBMap });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  InputBMap,
  openBMap
});
